package org.polyforms.repository.datanucleus;

import javax.persistence.EntityManager;

import org.datanucleus.jpa.NucleusJPAHelper;
import org.polyforms.repository.spi.EntityHelper;

/**
 * Implementation of {@link EntityHelper} for Datanucleus.
 * 
 * @author Kuisong Tong
 * @since 1.0
 */
public final class DatanucleusEntityHelper implements EntityHelper {
    private final EntityManager entityManager;

    /**
     * Create an instance with {@link EntityManager}.
     */
    public DatanucleusEntityHelper(final EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    /**
     * {@inheritDoc}
     */
    public boolean isDetached(final Object entity) {
        return NucleusJPAHelper.isDetached(entity);
    }

    /**
     * {@inheritDoc}
     */
    public boolean isEntity(final Class<?> candidate) {
        return DatanucleusUtil.getMetaData(entityManager, candidate) != null;
    }
}
